package com.six.work.controller;

import com.github.pagehelper.PageInfo;
import com.six.work.entity.Applicant;
import com.six.work.entity.Company;
import com.six.work.entity.Position;
import com.six.work.entity.Swords;
import com.six.work.services.ApplicantServices;
import com.six.work.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("app") //applicant的简写
public class ApplicantController {
    @Autowired
    private ApplicantServices applicantServices;

    //localhost:8080/app/list
    @GetMapping("/list")
    public Result findAllApplicant(@RequestParam(value = "applicant_name", defaultValue = "")String applicant_name,
                                   @RequestParam(value = "state_id", defaultValue = "0") Integer state_id){
        Result result = new Result();
        List<Applicant> lists=null;
        if (state_id == 0) {  //没有选择状态的时候
            lists = applicantServices.findAppByAppName(applicant_name);
        } else {
            lists = applicantServices.findAppByStateAndAppName(applicant_name,state_id);
        }
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(lists);
        return result;
    }

    @GetMapping("/list2")
    public Result findAllApplicantJob() {
        Result result = new Result();
        List<Applicant> list = applicantServices.findAllApplicantJob();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(list);
        return result;
    }

    //求职者发布管理查询,分页功能已完成
    @GetMapping("/list3/{num}")
    public Result findAppJobByName(@PathVariable("num") Integer num,
                                   @RequestParam(value="appName",defaultValue = "") String appName) {
        Result result = new Result();
        PageInfo<Applicant> pageInfo = applicantServices.findAppJobByName(appName,num);

        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(pageInfo);
        return result;
    }


    @GetMapping("/del")
    public Result deleteByID(@RequestParam("applicant_id") Integer applicant_id){
        Result result = new Result();
        int i  = applicantServices.DelAppByID(applicant_id);
        if(i==1) {
            result.setCode(200);
            result.setMessage("删除成功");
        }else{
            result.setCode(300);
            result.setMessage("删除失败");
        }
        return  result;
    }

    //求职者信息管理查询
    @GetMapping("/list1/{num}")
    public Result findAppByName(@PathVariable("num") Integer num,
                                @RequestParam(value="appName",defaultValue = "") String appName) {
        Result result = new Result();
        PageInfo<Applicant> pageInfo = applicantServices.findAppByName(appName,num);
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(pageInfo);
        return result;
    }



    @GetMapping("/update")
    public Result updateAppState(@RequestParam("applicant_id") Integer applicant_id) {
        Result result = new Result();
        String update = applicantServices.updateAppState(applicant_id);
        result.setObject(update);
        return result;
    }

    @GetMapping("/update1")
    public Result updateAppState1(@RequestParam("applicant_id") Integer applicant_id) {
        Result result = new Result();
        String update = applicantServices.updateAppState1(applicant_id);
        result.setObject(update);
        return result;
    }

    @PostMapping("/login")
    public Result UserLogin(@RequestBody Applicant applicant) {
        Result result = new Result();
        Applicant app = applicantServices.UserLogin(applicant.getApplicant_number());

        if (app != null) { //stu返回对象不为空则表示，用户名正确
            if (app.getApplicant_pass().equals(applicant.getApplicant_pass())) { //为真则表示，密码正确
                result.setCode(200);
            } else {  //为假则表示，密码错误
                result.setCode(201);
            }
        } else { //stu返回对象为空则表示，用户不正确
            result.setCode(202);
        }
        result.setObject(app);
        return result;
    }

    @PostMapping("/add")
    public Result InsertApp(@RequestBody Applicant applicant) {
        Result result = new Result();
        int i = applicantServices.insertApp(applicant);
        if(i==1){
            result.setCode(200);
            result.setMessage("添加成功");
        }else{
            result.setCode(300);
            result.setMessage("添加失败");
        }
        return result;
    }

    @GetMapping("/list4")
    public List<Applicant> findAllApplicant(){
        List<Applicant> list = applicantServices.findAllApplicant();
        return list;
    }

    @GetMapping("/update2")
    public Applicant getUpdatePage(@RequestParam("applicant_id") Integer applicant_id){

        return applicantServices.SelectAppByID(applicant_id);
    }

    @PostMapping("/update2")
    public Integer UpdateApplicant(@RequestBody Applicant applicant) {
        return applicantServices.updateApplicant(applicant);
    }

    @PostMapping("/update3")
    public Integer UpdateApp(@RequestBody Applicant applicant) {
        return applicantServices.updateApp(applicant);
    }


    //可视化
    @GetMapping("/all")
    public List<Applicant> findAllApp(){
        List<Applicant> list=applicantServices.findAllApp();
        return list;
    }
    //今日新增
    @GetMapping("/today")
    public Integer findTodayCom(){
        return applicantServices.FindTodayCom();
    }

    //本月新增
    @GetMapping("/month")
    public Integer findMonthCom(){
        return applicantServices.FindMonthCom();
    }
}
